package client.construct.singleton;

import org.junit.Test;
import uml.construct.singleton.EagerSingleton;
import uml.construct.singleton.IoDHSingleton;
import uml.construct.singleton.LazySingleton;

/**
 * 单例--客户端<br>
 * on 2022/12/21 23:47
 */
public class Client {
    @Test
    public void t() {
        System.out.println(EagerSingleton.getInstance() == EagerSingleton.getInstance());
        System.out.println(LazySingleton.getInstance() == LazySingleton.getInstance());
        System.out.println(IoDHSingleton.getInstance() == IoDHSingleton.getInstance());
    }

    //测试并发环境
    public static void main(String... args) {
        t1();
        t2();
    }

    /**
     * 懒汉式单例线程安全测试
     */
    public static void t1() {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(LazySingleton.getInstance() + Thread.currentThread().getName());
            }, "   t-" + i).start();
        }
    }

    /**
     * IoDH方式单例线程安全测试
     */
    public static void t2() {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(IoDHSingleton.getInstance() + Thread.currentThread().getName());
            }, "   t-" + i).start();
        }
    }
}
